﻿Public Class Category
    Public l As New Label
    Public stype As Integer = 0
    Private Sub Category_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'l.Text = "1"
        If stype <> 0 Then
            cbotype.SelectedValue = stype
        End If
    End Sub
    Private Sub showType()
        cbotype.ValueMember = ("TypeId")
        cbotype.DisplayMember = ("TypeName")
        cbotype.DataSource = cmd.ExecuteDataTable("SELECT TypeId,TypeName FROM Type")
    End Sub
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        showType()
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        If txtcate.Text.Trim = "" Then
            MessageBox.Show("Category Can't be Empty")
            txtcate.Focus()
            Exit Sub
        End If
        If l.Text = "" Then
            cmd.Parameters("@category") = txtcate.Text
            cmd.Parameters("@type") = cbotype.SelectedValue
            cmd.ExecuteNonQuery("INSERT INTO Category VALUES(@category,@type,1)")
        Else
            cmd.Parameters("@category") = txtcate.Text
            cmd.Parameters("@type") = cbotype.SelectedValue
            cmd.Parameters("@id") = l.Text
            cmd.ExecuteNonQuery("UPDATE Category SET CategoryName=@category,TypeId=@type WHERE CategoryId=@id")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class